.TH std::unique_ptr::release 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::unique_ptr::release \- std::unique_ptr::release

.SH Synopsis
   pointer release() noexcept;  \fI(since C++11)\fP
                                (constexpr since C++23)

   Releases the ownership of the managed object, if any.

   get() returns nullptr after the call.

   The caller is responsible for cleaning up the object (e.g. by use of get_deleter()).

.SH Parameters

   \fI(none)\fP

.SH Return value

   Pointer to the managed object or nullptr if there was no managed object, i.e. the
   value which would be returned by get() before the call.

.SH Example


// Run this code

 #include <cassert>
 #include <iostream>
 #include <memory>

 struct Foo
 {
     Foo() { std::cout << "Foo\\n"; }
     ~Foo() { std::cout << "~Foo\\n"; }
 };

 // Ownership of the Foo resource is transferred when calling this function
 void legacy_api(Foo* owning_foo)
 {
     std::cout << __func__ << '\\n';
     // [legacy code that no one understands or dares touch anymore]
     // [...]
     delete owning_foo;
 }

 int main()
 {
     std::unique_ptr<Foo> managed_foo(new Foo);
     // [code that might return or throw or some such]
     // [...]
     legacy_api(managed_foo.release());

     assert(managed_foo == nullptr);
 }

.SH Output:

 Foo
 legacy_api
 ~Foo

.SH See also

   get         returns a pointer to the managed object
               \fI(public member function)\fP
   get_deleter returns the deleter that is used for destruction of the managed object
               \fI(public member function)\fP
   reset       replaces the managed object
               \fI(public member function)\fP
